package com.eostoken.eos_wallet.net.common;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.eostoken.eos_wallet.net.exception.ApiException;
import com.eostoken.eos_wallet.net.response.BaseResponse;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.io.IOException;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;


public abstract class RxSubscriber2<T extends BaseResponse> implements Observer<T> {

    //    private LoadingDialog mLoadingDialog;
    private QMUITipDialog mLoadingDialog;
    private Disposable disposable;
    private Context context;
    private String errorMsg;
    private boolean mShowLoading = false;

    public RxSubscriber2(Context context, boolean showLoading) {
        this.context = context;
        this.mShowLoading = showLoading;
    }

    public RxSubscriber2() {
    }

    @Override
    public void onSubscribe(Disposable d) {
        disposable = new CompositeDisposable();
        if (mShowLoading) {
            showLoading();
        }
    }


    @Override
    public void onNext(T t) {
        if (!t.isSuccess()) {
            ApiException apiException = new ApiException(t.getResult(), t.getMessage());
            onError(apiException);
            return;
        }
        onSuccess(t);
    }

    @Override
    public void onError(Throwable e) {
        if (e instanceof IOException) {
            /** 没有网络 */
            errorMsg = "Please check your network status";
        } else if (e instanceof HttpException) {
            /** 网络异常，http 请求失败，即 http 状态码不在 [200, 300) 之间, such as: "server internal error". */
            errorMsg = ((HttpException) e).response().message();
        } else if (e instanceof ApiException) {
            /** 网络正常，http 请求成功，服务器返回逻辑错误 */
            errorMsg = ((ApiException) e).getDisplayMessage();
        } else {
            /** 其他未知错误 */
            errorMsg = !TextUtils.isEmpty(e.getMessage()) ? e.getMessage() : "unknown error";
        }

        dismissLoading();
       /* if (BuildConfig.DEBUG && !ObjectUtils.isEmpty(context)) {
            AlertDialog.Builder localBuilder = new AlertDialog.Builder(context);
            localBuilder.setTitle("提示");
            localBuilder.setMessage(errorMsg);
            localBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int paramAnonymousInt) {
                    dialogInterface.dismiss();
                    if (!disposable.isDisposed()) {
                        disposable.dispose();
                    }
                }
            });
            localBuilder.setCancelable(false).create();
            localBuilder.show();
        }*/
        onFail(errorMsg);
    }


    public void onFail(String failMsg) {

    }

    @Override
    public void onComplete() {
        dismissLoading();
        Log.d("print", "-->执行了完成的方法");
    }

    public abstract void onSuccess(T t);


    private void showLoading() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new QMUITipDialog.Builder(context)
                    .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                    .setTipWord("正在加载")
                    .create();
        }
        mLoadingDialog.show();
//        if (mLoadingDialog == null) {
//            mLoadingDialog = new LoadingDialog();
//            mLoadingDialog.setOutCancel(false);
//        }
//        mLoadingDialog.show(context.getSupportFragmentManager());
    }

    private void dismissLoading() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }
}

